package com.avengers.core.kit;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author yu.wang
 * @since 2018-05-18 下午2:33
 */
public class RegexKit {
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String EASY_YEAR_DATE_FORMAT = "yy-MM-dd";
    public static final String DATE_FORMAT_WITHOUT_SYMBOL = "yyyyMMdd";
    public static final String EASY_YEAR_DATE_FORMAT_WITHOUT_SYMBOL = "yyMMdd";
    public static final String DATE_FORMAT_WITHOUT_YEAR = "MM-dd";
    public static final String DATE_FORMAT_WITHOUT_YEAR_AND_SYMBOL = "MMdd";
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_TIME_FORMAT_WITHOUT_SYMBOL = "yyyyMMddHHmmss";
    public static final String DATE_TIME_FORMAT_WITHOUT_SYMBOL_2 = "yyMMddHHmmss";
    public static final String DATE_MILLIS_FORMAT_WITHOUT_SYMBOL = "yyyyMMddHHmmssSSS";
    public static final String DATE_MILLIS_FORMAT_WITHOUT_SYMBOL_2 = "yyMMddHHmmssSSS";

    public static final String DATE_MILLIS_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";

    public static final String TIME_FORMAT = "HH:mm:ss";

    public static final String TIME_MILLIS_FORMAT = "HH:mm:ss.SSS";

    public static final String FLOAT = "(^([1-9][0-9]{0,10})(\\.[0-9]{0,2})?$)|([0].[0-9]{0,2})";

    public static final String INTEGER = "^[1-9][0-9]*";

    public static final String WITHOUT_NUMBER = "[^0-9.]";
    public static final String NUMBER = "[0-9]*";

    public static final String POSTCODE = "^[0-9]{3,6}$";

    public static final String MOBILE = "^[+]?[0-9]{0,4}1[3|4|5|6|7|8|9][0-9]{9}$";

    public static final String EMAIL = "\\b(^['_A-Za-z0-9-]+(\\.['_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\\.[A-Za-z0-9-]+)*((\\.[A-Za-z0-9]{2,})|(\\.[A-Za-z0-9]{2,}\\.[A-Za-z0-9]{2,}))$)\\b";

    public static final String YES_NO = "^YES|NO$";

    public static final String DOMAIN_NAME = "^\\s*[0-9a-zA-Z\\u4e00-\\u9fa5]+([\\-]?[0-9a-zA-Z\\u4e00-\\u9fa5])*(\\.[a-zA-Z\\u4e00-\\u9fa5]{2,12}){1,2}\\s*$";

    public static final String IP = "(2(5[0-5]{1}|[0-4]\\d{1})|[0-1]?\\d{1,2})(\\.(2(5[0-5]{1}|[0-4]\\d{1})|[0-1]?\\d{1,2})){3}";

    /**
     * 身份证
     */
    public static final String ID_CARD = "^\\d{17}(\\d|[xX])$";
    /**
     * 中文
     */
    public static final String CHINESE = "[\u4e00-\u9fa5]";
    public static final String SPACE_TAB = "[ \t]";
    public static final String SPACE_TAB_COMMA = "[ \t,]";
    public static final String SPACE_TAB_COMMA_ZH = "[ \t,，]";
    public static final String SPACE_TAB_N = "[ \t\n]";
    public static final String SPACE_TAB_R = "[ \t\r]";
    public static final String SPACE_TAB_N_R = "[ \t\n\r]";

    /**
     * 包含中文字符
     */
    private static final Pattern CHINESE_PATTERN = Pattern.compile(CHINESE);
    /**
     * 纯汉字正则
     */
    private static final Pattern ALL_CHINESE_PATTERN = Pattern.compile("^[\\u4E00-\\u9FFF]+$");

    private static final Pattern IP_PATTERN = Pattern.compile(IP);

    public static boolean contains(String str, String regex) {
        Matcher m = Pattern.compile(regex).matcher(str);
        return m.find();
    }

    /**
     * 校验匹配格式
     *
     * @param regex 正则表达式
     * @param str   待校验字符串
     * @return 校验结果
     */
    public static boolean match(String regex, String str) {
        return Pattern.compile(regex).matcher(str).matches();
    }

    /**
     * 校验不匹配格式
     *
     * @param regex 正则表达式
     * @param str   待校验字符串
     * @return 校验结果
     */
    public static boolean notMatch(String regex, String str) {
        return !match(regex, str);
    }

    /**
     * 判断是否纯中文
     */
    public static boolean isChinese(String word) {
        return ALL_CHINESE_PATTERN.matcher(word).matches();
    }

    public static boolean hasChinese(String str) {
        Matcher m = CHINESE_PATTERN.matcher(str);
        return m.find();
    }

    public static boolean isIp(String str) {
        Matcher m = IP_PATTERN.matcher(str);
        return m.matches();
    }

    public static boolean notIp(String str) {
        return !isIp(str);
    }
}
